<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <j-popup
                v-model="model.name"
                code="query_record_product"
                field="name"
                :param="popParam"
                :triggerChange="true"
                orgFields="id,name,number,unit_price,specifications,unit"
                destFields="productId,name,number,unitPrice,specifications,unit"
                @callback="onPopCellChange(model.key, 'products',$event)"
              >
              </j-popup>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number">
              <a-input disabled v-model="model.number" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="specifications">
              <a-input v-model="model.specifications" placeholder="请输入规格" />

            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="退货单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <j-dict-select-tag type="list" v-model="model.unit" dictCode="product_unit" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="退货数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="num">
              <a-input-number placeholder="请输入数量" v-model="model.num" class="w100"
                              @blur="quotedPriceTotalComputer"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="退货单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unitPrice">
              <a-input-number placeholder="请输入单价" v-model="model.unitPrice" class="w100"
                              @blur="quotedPriceTotalComputer"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span=" 24">
            <a-form-model-item label="单价小计" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quotedPriceTotal">
              <a-input-number v-model="model.quotedPriceTotal" disabled class="w100" ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
              <a-input v-model="model.remarks" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>

        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDepCustom'
import moment from 'moment'
import JPopup from '@comp/jeecg/JPopup'

export default {
  name: 'ProductInfoForm',
  components: {
    JPopup,
    JSelectUserByDep
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      popParam:{ productIds:'' },
      productRecordIds:'',//产品id集合
      ckeckPrice:0,//合同金额
      checkNum:0,//合同数量
      isRefund:1,//
      model: {
        productId: '',//产品ID
        name: '',//产品名称
        number: '',//编号
        specifications: '',//规格
        unit: '',//报价单位
        num: 1,//数量
        unitPrice: '',//单价
        quotedPriceTotal: '',//报价小计
        remarks: '',//备注
        isRefund:1,
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        name: [
          { required: true, message: '请选择产品!' }
        ],
        unitPrice: [
          { required: true, message: '请输入退款单价!' },
          {
            validator: (rule, value, callback) => {
              if (Number(value) > this.ckeckPrice) {
                callback(new Error(`退款单价不能超过${this.ckeckPrice}`))
              } else {
                callback()
              }
            },
            trigger: ['blur']
          }
        ],
        num: [
          { required: true, message: '请输入退货数量!' },
          {
            validator: (rule, value, callback) => {
              if (Number(value) > this.checkNum) {
                callback(new Error(`退货数量不能超过${this.checkNum}`))
              } else {
                callback()
              }
            },
            trigger: ['blur']
          }
        ]
      },
      url: {
        add: "/refundreturn/refundReturn/add",
        edit: "/refundreturn/refundReturn/edit",
        queryById: "/refundreturn/refundReturn/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    moment,
    //总价
    quotedPriceTotalComputer() {
      this.model.quotedPriceTotal = this.model.num * this.model.unitPrice
    },
    onPopCellChange(key, dataIndex, value) {
      console.log('key', key)
      console.log('dataIndex', dataIndex)
      console.log('value', value)
      //产品ID
      this.model.productId  = value.productId
      //名称
      this.model.name = value.name
      //编号
      this.model.number = value.number
      //规格
      this.model.specifications = value.specifications
      //报价单位
      this.model.unit = value.unit
      //单价
      this.model.unitPrice = value.unitPrice
      //单价小计
      this.model.quotedPriceTotal  = this.model.unitPrice * this.model.num
      this.productInfo()
    },
    productInfo(){
      const productIds =  this.popParam.productIds.split(',')
      productIds.forEach(e => {
        let productId = e
        getAction('/productrecord/productRecord/queryById?ids='+ productId ).then(res => {
          if (res.success) {
            let tempArr = res.result
            let cpId = tempArr.map(e => {
              return e.productId
            })
            let productRecordId = tempArr.map(e => {
              return e.id
            })
            this.model.productRecordIds = productRecordId.toString()
            this.model.isRefund = this.isRefund
            if(cpId == this.model.productId){
              this.checkNum = this.model.num = Number(
                tempArr.map(e => {
                  return e.num
                })
              )
              this.ckeckPrice = Number(
                tempArr.map(e => {
                  return e.unitPrice
                })
              )
            }
          }
        })
      })
    },
    add(e) {
      console.log('eeeeeeeeeeeeeeeeeeeeeeeeeee',e)
      this.productRecordIds = e
      this.popParam.productIds = this.productRecordIds.toString()
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      this.confirmLoading = true
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          this.confirmLoading = false
          this.$emit('ok', this.model)
        } else {
          this.confirmLoading = false
        }
      })
    }
  }
}
</script>
<style scoped lang="less">
.w100 {
  width: 100%;
}
</style>